<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form">
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="组织" prop="company">
              <j-dict-select-tag
                dictCode="crm_company,name,id"
                v-model="model.company"
                placeholder="请选择组织"
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车间" prop="workshop">
              <j-dict-select-tag placeholder="请选择车间" v-model="model.workshop" dictCode="workshop"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工序" prop="process">
              <j-dict-select-tag
                dictCode="working_procedure"
                v-model="model.process"
                placeholder="请选择工序"
                disabled
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="品种" prop="cproductType">
              <j-dict-select-tag placeholder="请选择品种" v-model="model.cproductType" @change="changePz"
                                 dictCode="crm_product_type_cotton,product_type,id"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="机号" prop="preSpinDeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.preSpinDeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="showModal('ShuMian')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="楼层" prop="floor">
              <j-dict-select-tag
                dictCode="floor"
                v-model="model.floor"
                placeholder="请选择楼层"
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A开台" prop="adeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.adeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="ktOpen('jakt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A品种">
              <j-dict-select-tag placeholder="请选择" v-model="model.acProductType"
                                 dictCode="crm_product_type_cotton,product_type,id"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A开始时间" prop="aStartTime">
              <a-space>
                <a-date-picker v-model:value="formAStartTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                               @change="changeAStartTime"/>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="甲A结束时间" prop="aEndTime">
              <a-date-picker v-model:value="formAEndTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                             @change="changeAEndTime"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B开台" prop="bdeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.bdeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="ktOpen('ybkt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B品种">
              <j-dict-select-tag placeholder="请选择" v-model="model.bcProductType"
                                 dictCode="crm_product_type_cotton,product_type,id"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B开始时间" prop="aStartTime">
              <a-space>
                <a-date-picker v-model:value="formBStartTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                               @change="changeBStartTime"/>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="乙B结束时间" prop="aEndTime">
              <a-date-picker v-model:value="formBEndTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                             @change="changeBEndTime"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C开台" prop="cdeviceIds">
              <a-space>
                <a-input placeholder="请选择" style="width:215px" readOnly v-model="model.cdeviceText"></a-input>
                <a-button type="primary" icon="plus" size="small" @click="ktOpen('bckt')"></a-button>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C品种">
              <j-dict-select-tag placeholder="请选择" v-model="model.ccProductType"
                                 dictCode="crm_product_type_cotton,product_type,id"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C开始时间" prop="cStartTime">
              <a-space>
                <a-date-picker v-model:value="formCStartTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                               @change="changeCStartTime"/>
              </a-space>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="丙C结束时间" prop="cEndTime">
              <a-date-picker v-model:value="formCEndTime" placeholder="选择日期" style="width:215px" :allowClear="false"
                             @change="changeCEndTime"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="临时计划" prop="temOutput">
              <a-input-number v-model:value="model.temOutput" style="width:215px"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设定车速" prop="speedSet">
              <a-input-number v-model:value="model.speedSet" style="width:215px"></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
    <shu-mian-ji-tai-modal ref="shumianModal" @getDevSnArr="getDevSnArr"></shu-mian-ji-tai-modal>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import ShuMianJiTaiModal from './ShuMianJiTaiModal'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'
import { message } from 'ant-design-vue'
import moment from 'moment';

export default {
  name: 'QinghuaModal',
  components: {
    JEditor,
    JSwitch,
    ShuMianJiTaiModal
  },
  data() {
    return {
      equipmentScopeText: '',
      formAStartTime: moment(new Date()).format('YYYY-MM-DD'),
      formAEndTime: moment(new Date()).format('YYYY-MM-DD'),
      formBStartTime: moment(new Date()).format('YYYY-MM-DD'),
      formBEndTime: moment(new Date()).format('YYYY-MM-DD'),
      formCStartTime: moment(new Date()).format('YYYY-MM-DD'),
      formCEndTime: moment(new Date()).format('YYYY-MM-DD'),
      adds: {
        org: '',
        cj: '',
        devSnArea: [],
        equipmentScope: [],
        ShuMian: [],
        equipmentScopeLt: [],
        lc: '',
        pz: '',
        pzCs: '',
        qk: 0,
        time: '',
        jakt: [],
        ybkt: [],
        pzja: '',
        pzyb: ''
      },
      title: '操作',
      visible: false,
      disable: true,
      model: {
        company: null,
        workshop: null,
        floor: null,
        preSpinDeviceText:'',
        preSpinDeviceIds:'',
        adeviceIds: '',
        adeviceText: '',
        acProductType: null,
        bdeviceIds: '',
        bdeviceText: '',
        bcProductType: null,
        cdeviceIds: '',
        cdeviceText: '',
        ccProductType: null,
        unOpen: 0,
        temOutput: null,
        speedSet: null,
        acStartTime: moment(new Date()).format('YYYY-MM-DD') + " 00:00:00",
        acEndTime: moment(new Date()).format('YYYY-MM-DD') + " 8:00:00",
        bcStartTime: moment(new Date()).format('YYYY-MM-DD') + " 8:00:00",
        bcEndTime: moment(new Date()).format('YYYY-MM-DD') + " 16:00:00",
        ccStartTime: moment(new Date()).format('YYYY-MM-DD') + " 16:00:00",
        ccEndTime: moment(new Date()).format('YYYY-MM-DD') + " 24:00:00",
        planTime: moment(new Date()).format('YYYY-MM-DD'),
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 9 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      rules: {
        company: [{ required: true, message: '请选择组织', trigger: 'blur' }],
        workshop: [{ required: true, message: '请选择车间', trigger: 'blur' }],
        process: [{ required: true, message: '请选择工序', trigger: 'blur' }],
        cproductType: [{ required: true, message: '请选择品种', trigger: 'blur' }],
        roveDeviceIds: [{ required: true, message: '请选择机号', trigger: 'blur' }],
        adeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        bdeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        cdeviceIds: [{ required: true, message: '请选择', trigger: 'blur' }],
        temOutput: [{ required: true, message: '请输入临时计划', trigger: 'blur' }],
        speedSet: [{ required: true, message: '请输入设定车速', trigger: 'blur' }]
      },
      url: {
        add: '/crm_daily_plan_pre_spin/crmDailyPlanPreSpin/add',
        edit: '/crm_daily_plan_pre_spin/crmDailyPlanPreSpin/edit'
      }
    }
  },
  created() {
  },
  methods: {
    add() {
      this.disable = false
      this.adds = {
        org: '',
        cj: '',
        devSnArea: [],
        equipmentScope: [],
        ShuMian: [],
        equipmentScopeLt: [],
        lc: '',
        pz: '',
        pzCs: '',
        qk: 0,
        time: '',
        jakt: [],
        ybkt: [],
        pzja: '',
        pzyb: ''
      }
      this.model.process = 2
      this.edit({}, 1)
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
      this.model.preSpinDeviceIds = ''
      this.model.preSpinDeviceText = ''
      this.model.adeviceIds = ''
      this.model.adeviceText = ''
      this.model.acProductType = null
      this.model.bdeviceIds = ''
      this.model.bdeviceText = ''
      this.model.bcProductType = null
      this.model.cdeviceIds = ''
      this.model.cdeviceText = ''
      this.model.ccProductType = null
      this.$refs.form.resetFields()
    },
    changeAStartTime(date, dateString){
      this.model.acStartTime = dateString + " 00:00:00"
    },
    changeAEndTime(date, dateString){
      this.model.acEndTime = dateString + " 8:00:00"
    },
    changeBStartTime(date, dateString){
      this.model.bcStartTime = dateString + " 8:00:00"
    },
    changeBEndTime(date, dateString){
      this.model.bcEndTime = dateString + " 16:00:00"
    },
    changeCStartTime(date, dateString){
      this.model.ccStartTime = dateString + " 16:00:00"
    },
    changeCEndTime(date, dateString){
      this.model.ccEndTime = dateString + " 24:00:00"
    },
    // 切换pz
    changePz(val) {
      this.add.pzja = val
      this.add.pzyb = val
      this.add.pzbc = val
      this.model.acProductType = val
      this.model.bcProductType = val
      this.model.ccProductType = val
    },
    handleOk() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    },
    showModal(param) {
      this.$refs.shumianModal.show(this.adds, param, true)
    },
    getDevSnArr(params, arr, choseDevideIds) {
      this.adds[params] = arr
      if (params == 'ShuMian') {
        this.model.preSpinDeviceText = this.getSeiresDevsnCs(arr)
        this.model.preSpinDeviceIds = choseDevideIds.join(',')
        if(this.model.adeviceIds == ''){
          this.adds['jakt'] = arr
          this.model.adeviceText = this.getSeiresDevsnCs(arr)
          this.model.adeviceIds = choseDevideIds.join(',')
        }
        if(this.model.bdeviceIds == ''){
          this.adds['ybkt'] = arr
          this.model.bdeviceText = this.getSeiresDevsnCs(arr)
          this.model.bdeviceIds = choseDevideIds.join(',')
        }
        if(this.model.cdeviceIds == ''){
          this.adds['bckt'] = arr
          this.model.cdeviceText = this.getSeiresDevsnCs(arr)
          this.model.cdeviceIds = choseDevideIds.join(',')
        }
      } else if (params == 'jakt') {
        this.model.adeviceText = this.getSeiresDevsnCs(arr)
        // this.model.adeviceText = this.getSeiresDevsn(arr);
        this.model.adeviceIds = choseDevideIds.join(',')
      } else if (params == 'ybkt') {
        // this.model.bdeviceText = this.getSeiresDevsn(arr);
        this.model.bdeviceText = this.getSeiresDevsnCs(arr)
        this.model.bdeviceIds = choseDevideIds.join(',')
      } else if (params == 'bckt') {
        // this.model.bdeviceText = this.getSeiresDevsn(arr);
        this.model.cdeviceText = this.getSeiresDevsnCs(arr)
        this.model.cdeviceIds = choseDevideIds.join(',')
      }
      this.calQk()
    },
    calQk() {
      // let devSn = this.adds.devSnArea.length
      // let ja = this.adds.jakt.length
      // let yb = this.adds.ybkt.length
      // this.adds.qk = devSn - (ja + yb) * 0.5
      // this.model.unOpen = devSn - (ja + yb) * 0.5
    },
    // 获取连续机台-粗纱
    getSeiresDevsnCs(arr) {
      let str = ''
      arr.map(item => {
        let itemStr = `${item.cj}：${this.getSeiresDevsn(item.devSnArea)}`
        str += itemStr + '；'
      })
      str = str.substring(0, str.length - 1)
      return str
    },
    // 获取连续车台
    getSeiresDevsn(arr) {
      if (arr) {
        var result = [],
          i = 0,
          str = ''
        if (arr.length > 0) {
          result[i] = [arr[0].devSn]
          arr.reduce(function (prev, cur) {
            parseInt(cur.devSn) - parseInt(prev.devSn) == 1
              ? result[i].push(cur.devSn)
              : (result[++i] = [cur.devSn])
            return cur
          })
          result.map(item => {
            if (item.length > 1) {
              str += `#${item[0]}~#${item[item.length - 1]},`
            } else {
              str += `#${item[0]},`
            }
          })
          str = str.substring(0, str.length - 1)
        } else {
          str = ''
        }
        return str
      }
    },
  }
}
</script>

<style scoped></style>
